const fs = require('fs');
const _ = require('lodash');
const config = require('./config.js');
let masterDataDriveBytes = 0n;
let masterDriveCount = 0;
let backupDataDriveBytes = 0n;
let backupDriveCount = 0;
let o = 0;
const masterDriveFiles = {}
const backupDriveFiles = {}

// 是否跳过目录
const noCompareDocuments = false;
// K、I是备份盘，比对主数据盘文件数量、字节数是否相同
// 对象的 length 和 Count可能不一样，是因为重名的原因
test('K:\\', true)('I:\\', true)('D:\\', false)('E:\\', false)('F:\\', false)('G:\\', false)('H:\\', false)('L:\\', false);
console.log('masterDataDriveBytes：', masterDataDriveBytes);
console.log('masterDriveCount：', masterDriveCount, Object.keys(masterDriveFiles).length);
console.log('backupDataDriveBytes：', backupDataDriveBytes);
console.log('backupDriveCount：', backupDriveCount, Object.keys(backupDriveFiles).length);
console.log(o);
console.log(masterDataDriveBytes == backupDataDriveBytes ? '相等' : '不相等');

for (const key of _.xor(Object.keys(masterDriveFiles), Object.keys(backupDriveFiles))) {
  console.log(masterDriveFiles[key] || backupDriveFiles[key])
}

function test(dir, flag){
  for (let video of fs.readdirSync(dir)) {

    if (config.skipDirectory.includes(video)) continue;

    const stat = fs.lstatSync(`${dir}${video}`);

    if (stat.isDirectory()) {
      if (noCompareDocuments) continue;
      test(`${dir}${video}\\`, flag);
    };

    const videoNameArr = video.split('.');
    // suffix = [ '视频1', 'mp4' ] 取最后一个元素，判断是不是视频类型
    const suffix = videoNameArr[videoNameArr.length - 1] .toLocaleLowerCase();
    if (!config.videoType.includes(suffix)) continue;

    // true是备份盘符，相加文件字节数
    if (flag) {
      backupDataDriveBytes += BigInt(stat.size);
      backupDriveCount += 1;
      if (masterDriveFiles[video]) o++;
      masterDriveFiles[video] = `${dir}${video}`
    } else {
      masterDataDriveBytes += BigInt(stat.size);
      masterDriveCount += 1;
      backupDriveFiles[video] = `${dir}${video}`
    };
  };
  return test
}